#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;

int flag = 1;

struct TreeNode
{
	char _ch;
	TreeNode* _left;
	TreeNode* _right;
};

int findroot(char* inorder, int size, char val) 
{
	for (int i = 0; i < size; i++)
		if (inorder[i] == val) return i;
	return -1;
}

TreeNode* buildTree(char* postorder, char* inorder, int n) 
{
	if (n <= 0) return nullptr;
	TreeNode* root = new TreeNode;
	root->_ch = postorder[n - 1];
	int k = findroot(inorder, n, root->_ch);
	if (k == -1)
	{
		flag = 0;
		return nullptr;
	}
	root->_left = buildTree(&postorder[0], &inorder[0], k);
	root->_right = buildTree(&postorder[k], &inorder[k + 1], n - k - 1);
	return root;
}




void PreOrderPrint(TreeNode* root)
{
	if (!root)
		return;
	cout << root->_ch;
	PreOrderPrint(root->_left);
	PreOrderPrint(root->_right);
}

int TreeHeight(TreeNode* root)
{
	if (root == nullptr)
		return -1;
	int left = TreeHeight(root->_left);
	int right = TreeHeight(root->_right);
	return left > right ? left + 1: right + 1;
}



int main()
{
	char in[27], post[27];
	while (cin >> post >> in)
	{
		int len = strlen(in);
		flag = 1;
		TreeNode* root = buildTree(post, in, len);
		if (flag == 1)
		{
			int height = TreeHeight(root);
			cout << height << endl;
			PreOrderPrint(root);
			cout << endl;
		}
		else
		{
			cout << "INVALID" << endl;
		}
	}

}